﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Hr_PersonnelTransferInfo。
    /// </summary>
    public class Hr_PersonnelTransferInfo
    {
        public Hr_PersonnelTransferInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_PersonnelTransferInfo(");
            strSql.Append("RecordId,UserId,UserName,CityId_O,City_O,CompanyId_O,Company_O,DepartId_O,Depart_O,Position_O,EmpLevel_O,CityId_N,City_N,CompanyId_N,Company_N,DepartId_N,Depart_N,Position_N,EmpLevel_N,TransType,TransReason,TransTime,AddTime,AddPerson)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@UserId,@UserName,@CityId_O,@City_O,@CompanyId_O,@Company_O,@DepartId_O,@Depart_O,@Position_O,@EmpLevel_O,@CityId_N,@City_N,@CompanyId_N,@Company_N,@DepartId_N,@Depart_N,@Position_N,@EmpLevel_N,@TransType,@TransReason,@TransTime,@AddTime,@AddPerson)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);

            db.AddInParameter(dbCommand, "CityId_O", DbType.Guid, model.CityId_O);
            db.AddInParameter(dbCommand, "City_O", DbType.AnsiString, model.City_O);

            db.AddInParameter(dbCommand, "CompanyId_O", DbType.Guid, model.CompanyId_O);
            db.AddInParameter(dbCommand, "Company_O", DbType.AnsiString, model.Company_O);

            db.AddInParameter(dbCommand, "DepartId_O", DbType.Guid, model.DepartId_O);
            db.AddInParameter(dbCommand, "Depart_O", DbType.AnsiString, model.Depart_O);
            db.AddInParameter(dbCommand, "Position_O", DbType.AnsiString, model.Position_O);
            db.AddInParameter(dbCommand, "EmpLevel_O", DbType.AnsiString, model.EmpLevel_O);

            db.AddInParameter(dbCommand, "CityId_N", DbType.Guid, model.CityId_N);
            db.AddInParameter(dbCommand, "City_N", DbType.AnsiString, model.City_N);

            db.AddInParameter(dbCommand, "CompanyId_N", DbType.Guid, model.CompanyId_N);
            db.AddInParameter(dbCommand, "Company_N", DbType.AnsiString, model.Company_N);

            db.AddInParameter(dbCommand, "DepartId_N", DbType.Guid, model.DepartId_N);
            db.AddInParameter(dbCommand, "Depart_N", DbType.AnsiString, model.Depart_N);
            db.AddInParameter(dbCommand, "Position_N", DbType.AnsiString, model.Position_N);
            db.AddInParameter(dbCommand, "EmpLevel_N", DbType.AnsiString, model.EmpLevel_N);
            db.AddInParameter(dbCommand, "TransType", DbType.AnsiString, model.TransType);
            db.AddInParameter(dbCommand, "TransReason", DbType.AnsiString, model.TransReason);
            db.AddInParameter(dbCommand, "TransTime", DbType.DateTime, model.TransTime);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Hr_PersonnelTransferInfo set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("UserName=@UserName,");

            strSql.Append("CityId_O=@CityId_O,");
            strSql.Append("City_O=@City_O,");

            strSql.Append("CompanyId_O=@CompanyId_O,");
            strSql.Append("Company_O=@Company_O,");

            strSql.Append("DepartId_O=@DepartId_O,");
            strSql.Append("Depart_O=@Depart_O,");
            strSql.Append("Position_O=@Position_O,");
            strSql.Append("EmpLevel_O=@EmpLevel_O,");

            strSql.Append("CityId_N=@CityId_N,");
            strSql.Append("City_N=@City_N,");

            strSql.Append("CompanyId_N=@CompanyId_N,");
            strSql.Append("Company_N=@Company_N,");

            strSql.Append("DepartId_N=@DepartId_N,");
            strSql.Append("Depart_N=@Depart_N,");
            strSql.Append("Position_N=@Position_N,");
            strSql.Append("EmpLevel_N=@EmpLevel_N,");
            strSql.Append("TransType=@TransType,");
            strSql.Append("TransReason=@TransReason,");
            strSql.Append("TransTime=@TransTime,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("AddPerson=@AddPerson");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);

            db.AddInParameter(dbCommand, "CityId_O", DbType.Guid, model.CityId_O);
            db.AddInParameter(dbCommand, "City_O", DbType.AnsiString, model.City_O);

            db.AddInParameter(dbCommand, "CompanyId_O", DbType.Guid, model.CompanyId_O);
            db.AddInParameter(dbCommand, "Company_O", DbType.AnsiString, model.Company_O);

            db.AddInParameter(dbCommand, "DepartId_O", DbType.Guid, model.DepartId_O);
            db.AddInParameter(dbCommand, "Depart_O", DbType.AnsiString, model.Depart_O);
            db.AddInParameter(dbCommand, "Position_O", DbType.AnsiString, model.Position_O);
            db.AddInParameter(dbCommand, "EmpLevel_O", DbType.AnsiString, model.EmpLevel_O);

            db.AddInParameter(dbCommand, "CityId_N", DbType.Guid, model.CityId_N);
            db.AddInParameter(dbCommand, "City_N", DbType.AnsiString, model.City_N);

            db.AddInParameter(dbCommand, "CompanyId_N", DbType.Guid, model.CompanyId_N);
            db.AddInParameter(dbCommand, "Company_N", DbType.AnsiString, model.Company_N);

            db.AddInParameter(dbCommand, "DepartId_N", DbType.Guid, model.DepartId_N);
            db.AddInParameter(dbCommand, "Depart_N", DbType.AnsiString, model.Depart_N);
            db.AddInParameter(dbCommand, "Position_N", DbType.AnsiString, model.Position_N);
            db.AddInParameter(dbCommand, "EmpLevel_N", DbType.AnsiString, model.EmpLevel_N);
            db.AddInParameter(dbCommand, "TransType", DbType.AnsiString, model.TransType);
            db.AddInParameter(dbCommand, "TransReason", DbType.AnsiString, model.TransReason);
            db.AddInParameter(dbCommand, "TransTime", DbType.DateTime, model.TransTime);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_PersonnelTransferInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,CityId_O,City_O,CompanyId_O,Company_O,DepartId_O,Depart_O,Position_O,EmpLevel_O,CityId_N,City_N,CompanyId_N,Company_N,DepartId_N,Depart_N,Position_N,EmpLevel_N,TransType,TransReason,TransTime,AddTime,AddPerson from Hr_PersonnelTransferInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,CityId_O,City_O,CompanyId_O,Company_O,DepartId_O,Depart_O,Position_O,EmpLevel_O,CityId_N,City_N,CompanyId_N,Company_N,DepartId_N,Depart_N,Position_N,EmpLevel_N,TransType,TransReason,TransTime,AddTime,AddPerson ");
            strSql.Append(" FROM Hr_PersonnelTransferInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }



        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,CityId_O,City_O,CompanyId_O,Company_O,DepartId_O,Depart_O,Position_O,EmpLevel_O,CityId_N,City_N,CompanyId_N,Company_N,DepartId_N,Depart_N,Position_N,EmpLevel_N,TransType,TransReason,TransTime,AddTime,AddPerson ");
            strSql.Append(" FROM Hr_PersonnelTransferInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo> list = new List<Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo model = new Chenxin.Lib.Hr.Entity.Hr_PersonnelTransferInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["UserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UserId = new Guid(ojb.ToString());
            }
            model.UserName = dataReader["UserName"].ToString();

            ojb = dataReader["CityId_O"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId_O = new Guid(ojb.ToString());
            }
            model.City_O = dataReader["City_O"].ToString();
            ojb = dataReader["CompanyId_O"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId_O = new Guid(ojb.ToString());
            }
            model.Company_O = dataReader["Company_O"].ToString();
            ojb = dataReader["CityId_N"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId_N = new Guid(ojb.ToString());
            }
            model.Depart_O = dataReader["City_N"].ToString();
            ojb = dataReader["CompanyId_N"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId_N = new Guid(ojb.ToString());
            }
            model.Company_N = dataReader["Company_N"].ToString();


            ojb = dataReader["DepartId_O"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId_O = new Guid(ojb.ToString());
            }
            model.Depart_O = dataReader["Depart_O"].ToString();
            model.Position_O = dataReader["Position_O"].ToString();
            model.EmpLevel_O = dataReader["EmpLevel_O"].ToString();
            ojb = dataReader["DepartId_N"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId_N = new Guid(ojb.ToString());
            }
            model.Depart_N = dataReader["Depart_N"].ToString();
            model.Position_N = dataReader["Position_N"].ToString();
            model.EmpLevel_N = dataReader["EmpLevel_N"].ToString();
            model.TransType = dataReader["TransType"].ToString();
            model.TransReason = dataReader["TransReason"].ToString();
            ojb = dataReader["TransTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TransTime = (DateTime)ojb;
            }
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

